package com.huanxing.cloud.mall.api.notify;

import cn.hutool.json.JSONUtil;
import com.huanxing.cloud.common.myabtis.tenant.HxTenantContextHolder;
import com.huanxing.cloud.mall.api.service.IOrderLogisticsService;
import com.kuaidi100.sdk.response.SubscribePushParamResp;
import com.kuaidi100.sdk.response.SubscribeResp;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单相关回调
 *
 * @author lijx
 * @since 2022/3/7 14:01
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/notify")
public class OrderNotify {

	private final IOrderLogisticsService orderLogisticsService;

	/**
	 * 订阅推送处理(参照) 订阅成功后，如果该快递单号有轨迹（包括已经签收的单），快递100将会在15分钟-4个小时推送；后面将会4个小时跟踪一次，跟踪到有轨迹变化则推送；
	 * 如果订阅成功后，3天查无结果（录错单/快递公司错了/揽收比较晚），快递100将会推送3天查无结果，可以继续重新订阅。
	 *
	 * <p>
	 * 回调接口支持自定义参数,比如订阅时回调地址填写的是 http://www.xxx.com?orderId=1233333 可以通过下面这种方式获取到orderId：
	 * String orderId = request.getParameter("orderId");
	 *
	 * <p>
	 * 返回值必须是下面这样的格式，否则快递100将认为该推送失败，快递100将会重试3次该推送，时间间隔35分钟； 成功结果返回例子：
	 * {"result":true,"returnCode":"200","message":"提交成功"}
	 * @param request
	 * @return: com.kuaidi100.sdk.response.SubscribeResp
	 * @author: lijx
	 * @time: 2022/6/11
	 */
	@ApiOperation(value = "物流信息回调")
	@PostMapping("/logistics")
	public SubscribeResp logisticsOrder(HttpServletRequest request) {
		String logisticsId = request.getParameter("logisticsId");
		String tenantId = request.getParameter("tenantId");
		SubscribePushParamResp subscribePushParamResp = JSONUtil.toBean(request.getParameter("param"),
				SubscribePushParamResp.class);
		log.info("物流回调:{}", subscribePushParamResp);
		HxTenantContextHolder.setCurrentTenantId(tenantId);
		SubscribeResp subscribeResp = new SubscribeResp();
		try {
			orderLogisticsService.notifyLogistics(logisticsId, subscribePushParamResp);
			subscribeResp.setResult(Boolean.TRUE);
			subscribeResp.setReturnCode("200");
			subscribeResp.setMessage("成功");
		}
		catch (Exception e) {
			subscribeResp.setResult(Boolean.FALSE);
			subscribeResp.setReturnCode("500");
			subscribeResp.setMessage(e.getMessage());
		}

		return subscribeResp;
	}

}
